home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / interp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  4.8 KB  |  192 lines

  1. /* $Id: interp.c,v 1.2 1996/09/25 02:01:28 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.0
  6.  * Copyright (C) 1995-1996  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: interp.c,v $
  26.  * Revision 1.2  1996/09/25 02:01:28  brianp
  27.  * removed gl_interp_texcoords() and gl_interp_texcoords2()
  28.  *
  29.  * Revision 1.1  1996/09/13 01:38:16  brianp
  30.  * Initial revision
  31.  *
  32.  */
  33.  
  34.  
  35. #include "interp.h"
  36. #include "macros.h"
  37. #include "types.h"
  38.  
  39.  
  40.  
  41. /*
  42.  * Linear integer interpolation of Z (depth) values:
  43.  * Iterpolate n integer values between z0 and z1 and put into zspan.
  44.  * When finished, zspan[0] = z0, zspan[n-1] = z1, and the rest of
  45.  * zspan[] is filled with interpolated values.
  46.  * We have to be careful to avoid integer overflow!
  47.  */
  48. void gl_interpolate_z( GLint n, GLint z0, GLint z1, GLdepth zspan[] )
  49. {
  50.    GLint i, dz;
  51.  
  52.    switch (n) {
  53.       case 1:
  54.          zspan[0] = z0;
  55.          return;
  56.       case 2:
  57.          zspan[0] = z0;
  58.          zspan[1] = z1;
  59.          return;
  60.       case 3:
  61.          zspan[0] = z0;
  62.          zspan[1] = (z0 + z1) >> 1;
  63.          zspan[2] = z1;
  64.          return;
  65.       default:
  66.          z0 = z0 << 7;
  67.          z1 = z1 << 7;
  68.          dz = (z1-z0) / (n-1);
  69.          for (i=0;i<n;i++) {
  70.             zspan[i] = z0 >> 7;
  71.             z0 += dz;
  72.          }
  73.          return;
  74.    }
  75. }
  76.  
  77.  
  78.  
  79. /*
  80.  * Linear integer interpolation:
  81.  * Iterpolate n integer values between y0 and y1 and put into yspan.
  82.  * When finished, yspan[0] = y0, yspan[n-1] = y1, and the rest of
  83.  * yspan[] is filled with interpolated values.
  84.  */
  85. void gl_interpolate_i( GLint n, GLint y0, GLint y1, GLint yspan[] )
  86. {
  87.    switch (n) {
  88.       case 1:
  89.          yspan[0] = y0;
  90.      return;
  91.       case 2:
  92.          yspan[0] = y0;
  93.          yspan[1] = y1;
  94.      return;
  95.       case 3:
  96.          yspan[0] = y0;
  97.      yspan[1] = (y0+y1) >> 1;
  98.          yspan[2] = y1;
  99.      return;
  100.       default:
  101.      if (y0==y1) {
  102.         register GLint i;
  103.         for (i=0;i<n;i++) {
  104.            yspan[i] = y0;
  105.         }
  106.      }
  107.      else {
  108.         register GLint i;
  109.         register GLint dx, dy;
  110.         register GLint a, b, d;
  111.         register GLint y;
  112.         register GLint qa, qb;
  113.            dx = n-1;
  114.         dy = y1 - y0;
  115.         qa = dy / dx;
  116.         dy = dy % dx;
  117.         if (dy<0) {
  118.            dy = -dy;
  119.            qb = qa - 1;
  120.         }
  121.         else {
  122.            qb = qa + 1;
  123.         }
  124.         a = dy+dy;   d = a-dx;   b = d-dx;
  125.         y = y0;
  126.         for (i=0;i<n;i++) {
  127.            yspan[i] = y;
  128.            if (d<0) {
  129.           d += a;
  130.           y += qa;
  131.            }
  132.            else {
  133.           d += b;
  134.           y += qb;
  135.            }
  136.         }
  137.      }
  138.    }
  139. }
  140.  
  141.  
  142.  
  143. /*
  144.  * Interpolate RGBA values.
  145.  * Input:  n - number of values to generate
  146.  *         r0, r1 - first and last alpha values
  147.  *         g0, g1 - first and last alpha values
  148.  *         b0, b1 - first and last alpha values
  149.  *         a0, a1 - first and last alpha values
  150.  * Output:  rspan, gspan, bspan, aspan - interpolated color values in
  151.  *          the range [0,CC.RedScale], [0,CC.GreenScale], [0,CC.BlueScale],
  152.  *          and [0,CC.AlphaScale].
  153.  */
  154. void gl_interpolate_rgba( GLint n,
  155.                           GLfixed r0, GLfixed r1, GLubyte rspan[],
  156.                           GLfixed g0, GLfixed g1, GLubyte gspan[],
  157.                           GLfixed b0, GLfixed b1, GLubyte bspan[],
  158.                           GLfixed a0, GLfixed a1, GLubyte aspan[] )
  159. {
  160.    GLint i, m;
  161.    GLfixed dr, dg, db, da;
  162.  
  163.    switch (n) {
  164.       case 1:
  165.          rspan[0] = FixedToInt(r0);
  166.          gspan[0] = FixedToInt(g0);
  167.          bspan[0] = FixedToInt(b0);
  168.          aspan[0] = FixedToInt(a0);
  169.      return;
  170.       case 2:
  171.          rspan[0] = FixedToInt(r0);   rspan[1] = FixedToInt(r1);
  172.          gspan[0] = FixedToInt(g0);   gspan[1] = FixedToInt(g1);
  173.          bspan[0] = FixedToInt(b0);   bspan[1] = FixedToInt(b1);
  174.          aspan[0] = FixedToInt(a0);   aspan[1] = FixedToInt(a1);
  175.      return;
  176.       default:
  177.          m = n-1;
  178.          dr = (r1-r0) / m;
  179.          dg = (g1-g0) / m;
  180.          db = (b1-b0) / m;
  181.          da = (a1-a0) / m;
  182.          for (i=0;i<n;i++) {
  183.             rspan[i] = FixedToInt(r0);    r0 += dr;
  184.             gspan[i] = FixedToInt(g0);    g0 += dg;
  185.             bspan[i] = FixedToInt(b0);    b0 += db;
  186.             aspan[i] = FixedToInt(a0);    a0 += da;
  187.          }
  188.          return;
  189.    }
  190. }
  191.  
  192.